#ifndef FOCUSSCAN_H
#define FOCUSSCAN_H

#include <QtCore/QThread>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtCore/QPoint>
#include <QtCore/QSemaphore>
#include "AutoFocusInterface.h"
#include <QTimer>

namespace Qly {

/**
 * @brief The FocusScan class  自动对焦类。这个类采用的策略是从高到低扫描一遍，将最清晰的图片存储下来。
 */
class FocusScan : public Qly::AutoFocusInterface
{
    Q_OBJECT
public:

    enum STATE {ON_IDLE, ON_MOVE_UP, ON_MOVE_UP_STOP, ON_MOVE_DOWN, ON_MOVE_DOWN_STOP, ON_GO_BACK, ON_GO_BACK_STOP};
    FocusScan(QObject *parent = nullptr);

    /**
     * @brief bestImage 获取当前最清晰的图像。通常是在扫描后调用这个函数。
     * @param image
     * @return 返回最清晰图像对应的评价值
     */
    double bestImage(QImage &image);

public slots:
    void startAutoFocus() override;
    void stop() override;

private slots:
    void newFocusValue(QImage& image, double score) override;
    void zMoveFinished(int32_t stopcode) override;
    void watchdogTimeout();
private:
    void startWatchDog(int ms = 6000);

    QImage m_best_image;
    double m_best_score = 0.0;
    double m_target_up_mm;
    double m_target_down_mm;

    STATE m_state = ON_IDLE;
    QTimer *m_pWatchDog = nullptr;
};

}

#endif // FOCUSSCAN_H
